package tableMaker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;


public class TableMaker {

	public static void main(String[] args) throws IOException {
		String[] dirs = {"results/dim10/lsc","results/dim11/lsc"};
		String[] drdirs = {"drresults/dim10/lsc","drresults/dim11/lsc"};
		String[] dirsint = {"instances/dim10", "instances/dim11"};
		String[] dirset = {"set/dim10", "set/dim11"};
		
		
		
		for(int dim = 0; dim < 2; dim++){
			int contset;
			HashMap<String, Integer> instances = new HashMap<String, Integer>();
			int continst  = 0;
			for(File file:new File(dirsint[dim]).listFiles() ){
				instances.put(file.toString(), continst++);
			}
			
			HashMap<Integer, Integer> kvalue = new HashMap<Integer, Integer>();
			kvalue.put(dim+10, 0);
			kvalue.put(0, 1);
			kvalue.put(1, 2);
			kvalue.put(-dim-10, 3);
			
			HashMap<String, Integer> set = new HashMap<String, Integer>();
			
			contset = 0;
			for(File file:new File(dirset[dim]).listFiles() ){
				set.put(file.toString(), contset++);
			}
			double[][][] solutions = new double[1000][100][5];
			double[][] media1 = new double[100][5];
			double[][] media2 = new double[100][5];

			FileReader filein;
			BufferedReader input;
			
			for(File file:new File(dirs[dim]).listFiles() ){
				filein = new FileReader(file);
				input = new BufferedReader(filein);
				TableTools.readResultFile(input, solutions, instances, kvalue, set, continst);
			}
			
			for(File file:new File(drdirs[dim]).listFiles() ){
				filein = new FileReader(file);
				input = new BufferedReader(filein);
				TableTools.readResultFileDR(input, solutions, instances, kvalue, set, continst);
			}
			
			//escrita das tabelas
			FileWriter resultsTOTALSet = new FileWriter("LSCSetDim="+ Integer.toString(dim+10) + ".tex", false);
			TableTools.initializeLatexFile(resultsTOTALSet, dim+10);
			FileWriter resultsTOTALInst = new FileWriter("LSCIntsDim="+ Integer.toString(dim+10) + ".tex", false);
			TableTools.initializeLatexFile(resultsTOTALInst, dim+10);
			
			for(int i = 0; i < continst; i++ ){
				for(int j = 0; j <= 4; j++){
					media1[i][j] = 0;
					for(int k = 0; k < contset; k++){
						media1[i][j] += solutions[i][k][j]/((double)contset);
					}
				}
			}
			
			for(int i = 0; i < contset; i++ ){
				for(int j = 0; j <= 4; j++){
					media2[i][j] = 0;
					for(int k = 0; k < continst; k++){
						media2[i][j] += solutions[k][i][j]/((double)contset);
					}
				}
			}
			
			
			for(File file:new File(dirsint[dim]).listFiles() ){
				String name = file.toString();
				int index = instances.get(name);
				resultsTOTALInst.append("{\\tt "+ name+"} &" + String.format("%.2f",media1[index][2]) + " & "+
				String.format("%.2f",((media1[index][0] - media1[index][2])/media1[index][2])*100) + " & " +
				String.format("%.2f",((media1[index][3] - media1[index][2])/media1[index][2])*100) + " & " +
				String.format("%.2f",((media1[index][4] - media1[index][2])/media1[index][2])*100) + "\\hline \\\\ \n");
			}
			resultsTOTALInst.append("\\end{tabular}");
			resultsTOTALInst.close();
			
			for(File file:new File(dirset[dim]).listFiles() ){
				String name = file.toString();
				int index = set.get(name);
				resultsTOTALSet.append("{\\tt "+ name+"} &" + String.format("%.2f",media2[index][2]) + " & "+
				String.format("%.2f",((media2[index][0] - media2[index][2])/media2[index][2])*100) + " & " +
				String.format("%.2f",((media2[index][3] - media2[index][2])/media2[index][2])*100) + " & " +
				String.format("%.2f",((media2[index][4] - media2[index][2])/media2[index][2])*100) + "\\hline \\\\ \n");
			}
			resultsTOTALSet.append("\\end{tabular}");
			resultsTOTALSet.close();
			
			
		}
		
		
		
		
	}
}

